﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TinyEngine.Core.Services;
using TinyEngine.Core.Asset;

namespace TinyEngine.Editor.DefaultPlugins.AssetExplorer
{
    /// <summary>
    /// Interaction logic for AssetChooser.xaml
    /// </summary>
    public partial class AssetChooser : UserControl
    {
        public AssetChooser()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(AssetChooser_Loaded);
        }

        void AssetChooser_Loaded(object sender, RoutedEventArgs e)
        {
            var assets = ServiceManager.GetService<AssetLibrary>();
            var root = new TreeViewItem()
            {
                Header = "Sprites"
            };
            treeView.Items.Add(root);

            foreach (var item in assets.Sprites)
            {
                var node=new TreeViewItem()
                {
                    Header = item.Key,
                    Tag = item.Value
                };
                root.Items.Add(node);
                node.MouseDoubleClick += new MouseButtonEventHandler(node_MouseDoubleClick);
            }
        }

        void node_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var item = ((TreeViewItem)treeView.SelectedItem).Tag;
            if (item != null)
            {
                SpriteChosen(item as SpriteAsset);
            }

        }

        public event Action<SpriteAsset> SpriteChosen;
    }
}
